import java.util.Scanner;

//数组中两个字符串的最小距离
public class test3 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int len = scanner.nextInt();//数组 str 的长度
        //输入
        String str1 = scanner.next();
        String str2 = scanner.next();
        String[] str = new String[len];


        for (int i = 0; i < len; i++) {
            str[i] = scanner.next();
        }

        //判断
        int result=choise(str1,str2,str);
        System.out.println(result);
    }

    public static int choise(String str1, String str2, String[] str){
        int flag1 = 0;//str1对比结果
        int flag2 = 0;//str2对比结果
        int min = str.length;
        for (int i = 0; i <str.length ; i++) {
            if (str[i].equals(str1)){
                flag1=i;
            }
            if (str[i].equals(str2)){
                flag2=i;
            }
            if(min>flag1-flag2){
                min = flag1-flag2;
                if(min<0){
                    min = -min;
                }
            }
        }
        if (flag1==0||flag2==0){
            min=-1;
        }
        return min;
    }
}
